package utils;

import java.awt.Color;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 *
 * @author chocoseb
 */
public class ChocoCalendar extends javax.swing.JPanel {

    //private ArrayList<DateSelection> _selection = new ArrayList<DateSelection>();
    private DateSelection _selected;
    private Calendar _beginDate = new GregorianCalendar();
    private Calendar _endDate = new GregorianCalendar();
    private Calendar _maxDate;
    private Calendar _minDate;
    private int _currentMonth = 0;
    private int _currentYear = 2011;

    /** Creates new form ChocoCalendraOneMonth */
    public ChocoCalendar() {
        initComponents();
        _minDate = ChocoDate.now();
        _minDate.set(Calendar.DATE, 1);
        _minDate.set(Calendar.MONTH, 0);
        _minDate.set(Calendar.YEAR, 1980);
        _maxDate = ChocoDate.now();
        _maxDate.set(Calendar.DATE, 1);
        _maxDate.set(Calendar.MONTH, 0);
        _maxDate.set(Calendar.YEAR, 2012);
        addYearToComboBox();
    }

//    public void setYear(int p_year) {
//        _oneYearNumber = p_year;
//    }
    public Calendar getMaxDate() {
        return _maxDate;
    }

    public void setMaxDate(Calendar _maxDate) {
        this._maxDate = _maxDate;
    }

    public Calendar getMinDate() {
        return _minDate;
    }

    public void setMinDate(Calendar _minDate) {
        this._minDate = _minDate;
    }

    public void setMonth(int p_month, int p_year) {
        panelCalendar.removeAll();
        Calendar temp = new GregorianCalendar();
        temp.set(Calendar.YEAR, p_year);
        temp.set(Calendar.MONTH, p_month);
        temp.set(Calendar.DATE, 1);
        cmbMonth.setSelectedIndex(temp.get(Calendar.MONTH));
        cmbYear.setSelectedIndex(temp.get(Calendar.YEAR) - _minDate.get(Calendar.YEAR));
        while (temp.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
            temp.set(Calendar.DATE, temp.get(Calendar.DATE) - 1);
        }
        setEntete();
        boolean monthIsEnded = false;
        _beginDate.setTime(temp.getTime());
        while (!monthIsEnded) {
            JToggleButton btn = new JToggleButton(String.valueOf(temp.get(Calendar.DATE)));
            if ((temp.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (temp.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)) {
                btn.setBackground(new Color(150, 150, 150));
            }
            if (temp.get(Calendar.MONTH) == p_month) {
                DateSelection selection = new DateSelection(temp, btn, this);
                selection.bindToToggleButton(btn);
                _selected = selection;
            } else {
                btn.setEnabled(false);
            }
            panelCalendar.add(btn);
            temp.set(Calendar.DATE, temp.get(Calendar.DATE) + 1);
            if (temp.get(Calendar.MONTH) > p_month || (p_month == 11 && temp.get(Calendar.MONTH) == 0)) {
                if (temp.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    monthIsEnded = true;
                }
            }
        }
        temp.set(Calendar.DATE, temp.get(Calendar.DATE) - 1);
        _endDate.setTime(temp.getTime());
        _currentMonth = p_month;
        _currentYear = p_year;
        this.validate();
    }

    public Calendar getSelectedDate() {
        if (_selected == null) {
            return null;
        }
        return _selected.getDate();
    }

    public DateSelection getSelectedSelection() {
        return _selected;
    }

    private void nextMonth() {
        if (_currentMonth < 11) {
            setMonth(_currentMonth + 1, _currentYear);
        } else {
            setMonth(0, _currentYear + 1);
        }
    }

    private void previousMonth() {
        if (_currentMonth > 0) {
            setMonth(_currentMonth - 1, _currentYear);
        } else {
            setMonth(11, _currentYear - 1);
        }

    }

    private void addYearToComboBox() {
        cmbYear.removeAllItems();
        for (int i = _minDate.get(Calendar.YEAR); i <= _maxDate.get(Calendar.YEAR); i++) {
            cmbYear.addItem(i);
        }
    }

    private void setEntete() {
        addLabelToEntete("Dim.");
        addLabelToEntete("Lun.");
        addLabelToEntete("Mar.");
        addLabelToEntete("Mer.");
        addLabelToEntete("Jeu.");
        addLabelToEntete("Ven.");
        addLabelToEntete("Sam.");
    }

    private void addLabelToEntete(String s) {
        JPanel pan = new JPanel();
        pan.setLayout(new FlowLayout());
        pan.setBackground(Color.LIGHT_GRAY);
        pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        JLabel lbl = new JLabel(s);
        pan.add(lbl);
        panelCalendar.add(pan);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCalendar = new javax.swing.JPanel();
        panelTop = new javax.swing.JPanel();
        lblPrevious = new javax.swing.JLabel();
        cmbMonth = new javax.swing.JComboBox();
        cmbYear = new javax.swing.JComboBox();
        linkNextMonth = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        panelCalendar.setLayout(new java.awt.GridLayout(0, 7));
        add(panelCalendar, java.awt.BorderLayout.CENTER);

        lblPrevious.setBackground(new java.awt.Color(204, 204, 204));
        lblPrevious.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrevious.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPrevious.setText("Previous");
        lblPrevious.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        lblPrevious.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblPrevious.setPreferredSize(new java.awt.Dimension(75, 18));
        lblPrevious.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblPreviousMouseClicked(evt);
            }
        });
        panelTop.add(lblPrevious);

        cmbMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" }));
        panelTop.add(cmbMonth);

        cmbYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001" }));
        cmbYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbYearActionPerformed(evt);
            }
        });
        panelTop.add(cmbYear);

        linkNextMonth.setBackground(new java.awt.Color(204, 204, 204));
        linkNextMonth.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        linkNextMonth.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        linkNextMonth.setText("Next");
        linkNextMonth.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        linkNextMonth.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        linkNextMonth.setPreferredSize(new java.awt.Dimension(75, 18));
        linkNextMonth.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                linkNextMonthMouseClicked(evt);
            }
        });
        panelTop.add(linkNextMonth);

        add(panelTop, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void linkNextMonthMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkNextMonthMouseClicked
        nextMonth();
    }//GEN-LAST:event_linkNextMonthMouseClicked

    private void lblPreviousMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblPreviousMouseClicked
        previousMonth();
    }//GEN-LAST:event_lblPreviousMouseClicked

    private void cmbYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbYearActionPerformed
//        if (cmbYear.getSelectedItem() != null) {
//            int selected = (Integer) cmbYear.getSelectedItem();
//            setMonth(_currentMonth, selected);
//        }
    }//GEN-LAST:event_cmbYearActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbMonth;
    private javax.swing.JComboBox cmbYear;
    private javax.swing.JLabel lblPrevious;
    private javax.swing.JLabel linkNextMonth;
    private javax.swing.JPanel panelCalendar;
    private javax.swing.JPanel panelTop;
    // End of variables declaration//GEN-END:variables
}
